var md5 = require('../../../../utils/md5.js');
var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    orderInfo: {
      type: Object,
      value: '',
      observer: function(newVal, oldVal, changedPath) {
        this.setData({
          orderId: newVal.id != undefined ? md5.b64Md5(newVal.id) : "",
          time: newVal.create_time != undefined ? util.formatTime(new Date(newVal.create_time)) : "",
          status: newVal.status,
        })
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    orderId: null, //订单号
    time: null, //发起时间
    user: true, //当前用户身份 true为导师
    status:-1,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    commit: function() {
      var myEventDetail = {
        msg: '来自component的信息'
      }
      var myEventOption = {}
      this.triggerEvent('myeventD', myEventDetail, myEventOption)
    },
    teacherConfirmOrder: function() {
      let that = this;
      wx.showModal({
        content: '是否确认上课完成？如未上课，可能会导致学员投诉哟～',
        cancelText: "再想想",//默认是“取消”
        confirmText: "是",//默认是“确定”
        success: function (res) {
          if (res.cancel) {
            return
          } else {
            let openid = wx.getStorageSync('userInfo').openid;
            util.request(api.TeacherConfirmOrder, {
              userOrderId: that.properties.orderInfo.id,
              openid: openid
            }, "PUT", false).then(function (res) {
                if(res.code == 200){
                  wx.showModal({
                    content: ' 恭喜，您已上课完成！课时费将在7日后自动打款至微信钱包。点击确定返回首页',
                    confirmText: "是",
                    complete: res => {
                      wx.switchTab({
                        url: '/pages/index/index',
                      });
                    },
                    showCancel: false
                  })
                }
            }).catch(e=>{
              wx.showToast({
                title: '操作失败',
                icon: 'none'
              });
            });
          }
        },
      })
    },
    waitEvalOrder: function() {
      let openid = wx.getStorageSync('userInfo').openid;
      let that = this
      util.request(api.WaitEvalOrder, {
        userOrderId: this.properties.orderInfo.id,
        openid: openid
      }, "PUT", false).then(function(res) {
        if (res.code === 200) {
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
          that.commit();
        } else {
          wx.showToast({
            title: '等待导师确认见面',
            icon: 'none'
          });
        }
      });
    },
    cancalOrder: function() {
      let openid = wx.getStorageSync('userInfo').openid;
      util.request(api.UserCancelOrder, {
        userOrderId: this.properties.orderInfo.id,
        openid: openid
      }, "PUT", false).then(function(res) {
        if (res.code === 200) {
          wx.showModal({
            content: '您已成功申请退款，点击确定返回首页',
            complete: res => {
              wx.switchTab({
                url: '/pages/index/index',
              });
            },
            showCancel: false
          })
        } else {
          wx.showToast({
            title: '退款失败,如果有疑问请联系客服人员',
            icon: 'none'
          });
        }
      });
    }
  },
  ready: function(e) {
    let user = false;
    if (this.properties.orderInfo.to_user_id == wx.getStorageSync('userInfo').id) {
      user = false;
    } else {
      user = true;
    }
    this.setData({
      user: user
    })

  }
})